package com.taoyi.mhouse.activities;


import android.os.PersistableBundle;
import android.support.annotation.NonNull;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;

import android.os.Bundle;

import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.RadioGroup;
import android.widget.Toast;


import com.taoyi.mhouse.R;
import com.taoyi.mhouse.fragments.FindingFragment;
import com.taoyi.mhouse.fragments.ForumFragment;
import com.taoyi.mhouse.fragments.HomeFragment;
import com.taoyi.mhouse.fragments.LoadingFragment;
import com.taoyi.mhouse.widget.BottomNavigation;

import java.util.ArrayList;
import java.util.List;

/**
 * @author tempo
 */
public class MainActivityIII extends BaseActivity implements HomeFragment.OnFragmentInteractionListener, BottomNavigation.OnPageChangedListener {

    private BottomNavigation mBottomNavigation;
    private HomeFragment mHomeFragment;
    private RetainFragment retainFragment;
    private int mPage = -1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main_activity_iii);
        retainFragment = RetainFragment.findOrCreateRetainFragment(getSupportFragmentManager());
        mHomeFragment = retainFragment.mHomeFragment;

        mBottomNavigation = (BottomNavigation) findViewById(R.id.mBottomNavigation);
        mBottomNavigation.setOnPageChangeListener(this);
        if (savedInstanceState != null) {
            showTag("HomeFragment=" + mHomeFragment);
            if (mHomeFragment == null) {
                mHomeFragment = new HomeFragment();
                retainFragment.mHomeFragment = mHomeFragment;
            }
            mPage = savedInstanceState.getInt("page", -1);
            if (mPage == -1)
                throw new NullPointerException("page is -1....be careful");
            mBottomNavigation.setInitPage(mPage);

        } else {
            if (mHomeFragment == null) {
                mHomeFragment = new HomeFragment();
                retainFragment.mHomeFragment = mHomeFragment;
            }
            mBottomNavigation.setInitPage(0);
        }
    }

    public void setViewPager(ViewPager vp) {
        mBottomNavigation.setViewPager(vp);
    }

    public Fragment getFragment(int i) {
        Fragment fragment;
        switch (i) {
            case 0:
                fragment = mHomeFragment;
                break;
            default:
                fragment = new FindingFragment();
                break;
        }
        return fragment;
    }

    @Override
    public void onSaveInstanceState(Bundle outState, PersistableBundle outPersistentState) {
        super.onSaveInstanceState(outState, outPersistentState);
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        outState.putInt("page", mPage);
        showTag("save page:" + mPage);
        super.onSaveInstanceState(outState);
    }

    @Override
    protected void onRestoreInstanceState(Bundle savedInstanceState) {
        super.onRestoreInstanceState(savedInstanceState);
    }

    @Override
    public void onPageChanged(int page) {
        if (mPage == page)
            return;
        Toast.makeText(this, "item:" + page, Toast.LENGTH_SHORT).show();
        FragmentManager manager = getSupportFragmentManager();
        FragmentTransaction transaction = manager.beginTransaction();
        transaction.replace(R.id.mContainer, getFragment(page));
        transaction.commit();
        mPage = page;
    }

    public static class RetainFragment extends Fragment {
        private static final String TAG = "RetainFragment";
        public HomeFragment mHomeFragment;

        public RetainFragment() {
        }

        public static RetainFragment findOrCreateRetainFragment(FragmentManager fm) {
            RetainFragment fragment = (RetainFragment) fm.findFragmentByTag(TAG);
            if (fragment == null) {
                fragment = new RetainFragment();
                fm.beginTransaction().add(fragment, TAG).commit();
            }
            return fragment;
        }

        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setRetainInstance(true);
        }
    }
}
